<template>
    <div class="sifter-box flex-row-center" v-if="isEdit==0">
        <el-form :inline="true" :model="sifterForm" class="demo-form-inline">
        </el-form>
        <div>
            <el-button type="primary" plain :loading="false" @click.prevent="editData('',1)">添加</el-button>
        </div>
    </div>

    <div class="listDiv" v-if="isEdit==0">
        <el-table :data="list" stripe style="width: 100%">
            <el-table-column prop="price" label="融资金额"> </el-table-column>
            <el-table-column prop="man" label="联系人"> </el-table-column>
            <el-table-column prop="mobile" label="联系电话"> </el-table-column>
            <el-table-column prop="msg" label="融资详情" width="100">
                <template #default="props">
                    <span class="pointer mr10 base-color" @click="editData(props.row,3)">查看详情</span>
                </template>
            </el-table-column>
            <el-table-column prop="" label="操作" width="100">
                <template #default="props">
                    <span v-if="props.row.isme!=0" class="pointer mr10" @click="editData(props.row,2)"><el-icon color="#409EFC"><edit/></el-icon></span>
                    <span v-if="props.row.isme!=0" class="pointer" @click="delData(props.row)"><el-icon color="#F56C6C"><delete /></el-icon></span>
                </template>
            </el-table-column>
        </el-table>
        <div class="pd20 w100 flex-row-center ju-flex-end">
            <el-pagination
                v-model:currentPage="sifterForm.currentpage"
                v-model:page-size="sifterForm.pagesize"
                :page-sizes="[10, 20, 50, 100]" :total="total"
                @size-change="()=>{updalist()}"
                @current-change="()=>{updalist()}"
                layout="total, sizes, prev, pager, next, jumper"  background/>
        </div>
    </div>

    <div class="edit-box" v-if="isEdit!=0">
        <div class="title-box flex-row-center">
            <span class="pointer return flex-row-center mr10" @click="re_turn"><el-icon><back /></el-icon></span>
            <span v-if="isEdit==1||isEdit==2">{{isEdit==1?'添加':'编辑'}}融资信息</span>
            <span v-if="isEdit==3">融资详情</span>
        </div>
        <div class="form-box">
            <el-form v-if="isEdit==1||isEdit==2" ref="ruleFormRef" :inline="true" :model="form" label-width="120px" class="demo-ruleForm">
                <el-form-item :rules="[ { required: true, message: '联系人不能为空'} ]" label="联系人" prop="man">
                    <el-input clearable v-model="form.man"></el-input>
                </el-form-item>
                <el-form-item :rules="[ { required: true, message: '联系电话不能为空'} ]" label="联系电话" prop="mobile">
                    <el-input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" clearable maxlength="11" v-model="form.mobile"></el-input>
                </el-form-item>
                <br/>
                <el-form-item :rules="[ { required: true, message: '融资金额不能为空'} ]" label="融资金额/元" prop="price">
                    <el-input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" maxlength="10" clearable v-model="form.price"></el-input>
                </el-form-item>
                <br/>
                <el-form-item :rules="[ { required: true, message: '详情不能为空'} ]" label="详情" prop="msg">
                    <Editer @update:value="(e)=>{form.msg = e}" :value="form.msg"/>
                </el-form-item>
                <el-form-item label=" " label-width="120px">
                    <el-button type="primary" class="btn" :loading="false" @click.prevent="submit" native-type="submit">提交</el-button>
                </el-form-item>
            </el-form>
            <div v-if="isEdit==3" class="">
                <span class="head-title mb10">融资金额：{{form.price||'0'}}元</span>
                <div class="flex-row-center ju-flex-start mt10 mb10">
                    <span class="base-size mr10 flex-row-center"><el-icon class="mr5"><user /></el-icon>{{form.man}}</span>
                    <span class="base-size mr10 flex-row-center"><el-icon class="mr5"><phone /></el-icon>{{form.mobile}}</span>
                </div>
                <div class="line mt10 mb10"></div>
                <div v-html="form.msg"></div>
            </div>
        </div>
    </div>

</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRefs,toRaw } from 'vue'
    import { Back,Edit,Delete,Timer,Phone,User,Position } from '@element-plus/icons-vue'
    import Editer from '../../components/editer.vue'

    let { proxy } = getCurrentInstance()
    let obj = reactive({
        isEdit: 0,
        total: 0,
        list:[],
        sifterForm: {
            currentpage: 1,
            pagesize: 10
        },
        form: {
            "docking_id":"",
            "man":"",
            "mobile":"",
            "price":"",
            "msg":""
        },
        ruleFormRef: null,
        types: []
    });

    // 获取资讯类别
    proxy.$utils.getNewsType(proxy).then(res=>{
        types.value = res
    })

    const updalist = (form)=>{
        let param = {
            ...obj.sifterForm,
            ...form
        }
        proxy.$ajax({url: 'docking/list',data: param,bfLoading: true}).then(d => {
            console.log(d);
            if(d.success){
                total.value = d.msg.count||0
                list.value = d.msg.list;
            }else{}
        }).catch(e => {
            console.log(e)
            if(e == "登录已过期，请重新登录"){
                proxy.routerPushMixin('/')
            }
        })
    }
    updalist();

    const datePickerChange = (e)=>{
        if(e&&e.length==2){
            sifterForm.value.starttime = e[0]
            sifterForm.value.endtime = e[1]
            updalist(obj.sifterForm)
        }
    }

    const delData = (item)=>{
            proxy.$ajax({url: 'docking/delete',data:{docking_id:item.docking_id},bfLoading: true}).then(d => {
                console.log(d);
                if(d.success){
                    proxy.elMessageMixin('删除成功！', 'success')
                    updalist()
                }else{proxy.elMessageMixin(date.err, 'error')}
            }).catch(e => {
                if(e == "登录已过期，请重新登录"){
                    proxy.routerPushMixin('/')
                }
            })
    }

    const re_turn = ()=>{
        isEdit.value = 0;
        form.value = {
            "docking_id":"",
            "man":"",
            "mobile":"",
            "price":"",
            "msg":""
        }
    }

    const editData = (item,isEditidx)=>{
        console.log(item);
        form.value = {
            ...item,
        }
        isEdit.value = isEditidx
    }

    const submit = ()=>{
      if (ruleFormRef) {
        ruleFormRef.value.validate((valid) => {
          if (valid) {
                console.log(form.value);
                let param = {
                    ...form.value,
                    docking_id:form.value.docking_id||0
                }
                // return
                proxy.$ajax({url: 'docking/design',data:param,bfLoading: true}).then(d => {
                    console.log(d);
                    if(d.success){
                        form.value = {
                            "docking_id":"",
                            "man":"",
                            "mobile":"",
                            "price":"",
                            "msg":""
                        }
                        proxy.elMessageMixin('编辑成功！', 'success')
                        isEdit.value = 0
                        updalist()
                    }else{proxy.elMessageMixin(date.err, 'error')}
                }).catch(e => {
                    console.log(e)
                    if(e == "登录已过期，请重新登录"){
                        proxy.routerPushMixin('/')
                    }
                })
          } else {
            return false;
          }
        });
      }
    }
    
    let { list, sifterForm, form, isEdit, ruleFormRef, types, total } = toRefs(obj)
</script>

<style lang="scss" scoped>
</style>